package com.songcha.module_home.ui.fragment.homehome

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreMvvmFragment
import com.songcha.library_base.mvvm.base.BaseRefreshMvvmFragment
import com.songcha.library_business.event.home.BookClickEvent
import com.songcha.library_business.helper.UMengHelper
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.module_home.R
import com.songcha.module_home.ui.adapter.HomeHomeAdapter
import com.songcha.module_home.databinding.HomeFragmentHomeHomeBinding
import org.greenrobot.eventbus.EventBus

class HomeHomeFragment : BaseRefreshLoadMoreMvvmFragment<HomeHomeViewModel, HomeFragmentHomeHomeBinding>(),IOnBookClickListener,HomeHomeAdapter.IOnEditorRecommendRefreshListener {
    private var mAdapter:HomeHomeAdapter?=null

    override fun getLayoutId(): Int {
        return R.layout.home_fragment_home_home
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        UMengHelper.onUMEvent(requireContext(),"page_main_load")

        viewModel.editorRecommendState.observe(this){data->
            if(mAdapter == null || mAdapter!!.data.size<5) return@observe
            if(data){
                val majiaName = MajiaBaoUtil.getMajiaName()
                if(majiaName == "douyue"){
                    mAdapter?.notifyItemChanged(1)
                }else{
                    mAdapter?.notifyItemChanged( mAdapter!!.data.size-1)
                }
            }else{
                //ToastUtil.show("没有更多数据了")
            }
        }

        viewModel.getHomeHomeDataState.observe(this){data->
            if(data){

            }else{
                UMengHelper.onUMEvent(requireContext(),"page_main_load_error")
            }
        }

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        srl?.apply{
            val majiaName = MajiaBaoUtil.getMajiaName()
            setEnableLoadMore(majiaName == "douyue")
            setOnLoadMoreListener {
                viewModel.onLoadingMore()
                onGetLoadMoreData()
            }
        }
    }

    override fun onGetData() {
        viewModel.getHomeHomeData()
    }

    override fun onGetLoadMoreData() {
        viewModel.getHostBookList()
    }

    //TODO
    override fun initLoadMoreListData() {

    }

    override fun onLoadMoreSuccess(start: Int, count: Int) {
        srl?.finishLoadMore(true)
        mAdapter?.notifyBookHostListItem(start,count)
        //getListAdapter()?.notifyItemRangeChanged(start,count)
    }

    override fun initListData() {
        databinding.homeHomeRcv.apply {
            layoutManager = LinearLayoutManager(context)
            mAdapter = HomeHomeAdapter(requireActivity(), this@HomeHomeFragment,viewModel.rlst.value!!,this@HomeHomeFragment,this@HomeHomeFragment)
            adapter = mAdapter
        }
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
       return databinding.homeHomeSrl
    }

    override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        if(mAdapter==null) return null
        return mAdapter as RecyclerView.Adapter<BaseViewHolder>
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun onRetry() {
        super.onRetry()
        viewModel.getHomeHomeData()
    }

    /*private fun initRcv(list:MutableList<HomeMultiItemType>){
        databinding.homeHomeRcv.apply {
            layoutManager=LinearLayoutManager(context)
            mAdapter=HomeHomeAdapter(this@HomeHomeFragment,list,this@HomeHomeFragment,this@HomeHomeFragment)
            adapter=mAdapter
        }
    }
*/
    override fun onBookClick(bookId: String,img:ImageView) {
       //BusinessJumpUtil.toBookDetail(bookId)
        //databinding.homeBov.startBookOpenAnimation(bookId,img,arr[0],arr[1])
        EventBus.getDefault().post(BookClickEvent(bookId,img))
    }

    override fun onRecommendRefresh() {
        viewModel.getEditorRecommend()
    }


    /* override fun getRefreshLayout(): SmartRefreshLayout {
        return databinding.homeHomeSrl
     }

     override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        if(mAdapter==null) return null
         return mAdapter as RecyclerView.Adapter<BaseViewHolder>
     }*/


}